﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GroupPage.xaml.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   A basic page that provides characteristics common to most applications.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace mPassRT.Pages
{
    using System;
    using System.Linq;

    using Windows.Foundation;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;

    using mPassRT.Utilities;
    using mPassRT.ViewModels;
    using mPassRT.ViewModels.Databases;
    using mPassRT.ViewModels.Group;

    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class GroupPage
    {
        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="GroupPage"/> class.</summary>
        public GroupPage()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>Gets the view model.</summary>
        /// <returns>The <see cref="DatabasesViewModel"/>.</returns>
        private GroupViewModel GetViewModel()
        {
            return (GroupViewModel)this.DataContext;
        }

        /// <summary>Called when item is clicked.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private async void OnItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem is NewItemViewModel == false)
            {
                this.GetViewModel().OpenCommand.Execute(e.ClickedItem);
                return;
            }

            var menu = new PopupMenu();
            menu.Commands.Add(new UICommand("New Group", _ => this.GetViewModel().AddNewGroupCommand.Execute(null)));
            menu.Commands.Add(new UICommand("New Entry", _ => this.GetViewModel().AddNewEntryCommand.Execute(null)));

            var gridView = (GridView)sender;
            var addButtonContainer =
                (FrameworkElement)gridView.ItemContainerGenerator.ContainerFromItem(gridView.Items.Last());

            // get rectangle of the + button
            Rect containerRect = addButtonContainer.GetFrameworkElementRectangle();

            // adjust rectangle
            var iconSize = (double)this.Resources["IconSize"];
            var margin = (Thickness)this.Resources["IconMargin"];
            containerRect.Width = iconSize + margin.Left + margin.Right;

            await menu.ShowForSelectionAsync(containerRect, Placement.Right);
        }

        /// <summary>Responds to selection changes.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnItemSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var gridView = (GridView)sender;
            NewItemViewModel newItem = e.AddedItems.OfType<NewItemViewModel>().FirstOrDefault();
            if (newItem != null)
            {
                ((GridView)sender).SelectedItems.Remove(newItem);
                return;
            }

            this.GetViewModel().SelectedItems = gridView.SelectedItems.OfType<ItemViewModel>().ToList();
        }

        #endregion
    }
}